package com.ruoyi.station.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.station.domain.*;
import com.ruoyi.station.service.*;
import com.ruoyi.station.util.ParseSql;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/homePage")
@Api(value="首页数据", tags = "首页数据")
public class SHomePageController {
    @Autowired
    private SStationService sStationService;
    @Autowired
    private SMachineRoomService sMachineRoomService;
    @Autowired
    private SMachineCabinetService sMachineCabinetService;
    @Autowired
    private SEquipmentService sEquipmentService;
    @Autowired
    private SOpticalCableService sOpticalCableService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private SDistributionFrameService sDistributionFrameService;
    @GetMapping("/getHomePageData")
    @ApiOperation(value="查询首页数据", notes="查询首页数据")
    public AjaxResult getHomePageData(@ApiParam String area){
        AjaxResult ajaxResult = new AjaxResult();
        //通过部门ID获取同部门的所有员工
        List<String> userList = iSysUserService.selectUserByDept(SecurityUtils.getLoginUser().getUser().getDeptId());
        //属于当前角色的用户
        Set<String> userByRoleSet = ParseSql.getUserByRoleUtil(userList);
        List<SStation> SStationList;
        List<SOpticalCable> SOpticalCableList;
        if (StrUtil.isNotEmpty(area)){
            if (userByRoleSet != null && userByRoleSet.size() >0){
                SStationList = sStationService.list(new LambdaQueryWrapper<SStation>().eq(SStation::getArea, area).in(SStation::getCreateBy , userByRoleSet));
                SOpticalCableList = sOpticalCableService.list(new LambdaQueryWrapper<SOpticalCable>().eq(SOpticalCable::getArea,area).in(SOpticalCable::getCreateBy , userByRoleSet));
            }else {
                SStationList = sStationService.list(new LambdaQueryWrapper<SStation>().eq(SStation::getArea, area));
                SOpticalCableList = sOpticalCableService.list(new LambdaQueryWrapper<SOpticalCable>().eq(SOpticalCable::getArea,area));
            }
        }else {
            if (userByRoleSet != null && userByRoleSet.size() >0){
                SStationList = sStationService.list(new LambdaQueryWrapper<SStation>().in(SStation::getCreateBy , userByRoleSet));
                SOpticalCableList = sOpticalCableService.list(new LambdaQueryWrapper<SOpticalCable>().in(SOpticalCable::getCreateBy , userByRoleSet));
            }else{
                SStationList = sStationService.list();
                SOpticalCableList = sOpticalCableService.list();
            }
        }
        ajaxResult.put("stationInfo",SStationList);
        ajaxResult.put("station",SStationList.size());
        ajaxResult.put("opticalCable",SOpticalCableList.size());

        List<SMachineCabinet> machineCabinetList = new ArrayList<>();
        int machineRoomCount = 0;
        List<String> stationIdList = SStationList.stream().map(SStation::getId).collect(Collectors.toList());
        if (stationIdList != null && stationIdList.size() > 0){
            if (userByRoleSet != null && userByRoleSet.size() >0){
                machineRoomCount = sMachineRoomService.count(new LambdaQueryWrapper<SMachineRoom>().in(SMachineRoom::getStation, stationIdList).in(SMachineRoom::getCreateBy , userByRoleSet));
                machineCabinetList = sMachineCabinetService.list(new LambdaQueryWrapper<SMachineCabinet>().in(SMachineCabinet::getStation, stationIdList).in(SMachineCabinet::getCreateBy , userByRoleSet));
            }else{
                machineRoomCount = sMachineRoomService.count(new LambdaQueryWrapper<SMachineRoom>().in(SMachineRoom::getStation, stationIdList));
                machineCabinetList = sMachineCabinetService.list(new LambdaQueryWrapper<SMachineCabinet>().in(SMachineCabinet::getStation, stationIdList));
            }
        }
        ajaxResult.put("machineRoom",machineRoomCount);
        ajaxResult.put("machineCabinet",machineCabinetList.size());

        int equipmentCount = 0;
        List<String> machineCabinetIdList = machineCabinetList.stream().map(SMachineCabinet::getId).collect(Collectors.toList());
        if (machineCabinetIdList != null && machineCabinetIdList.size() > 0){
            if (userByRoleSet != null && userByRoleSet.size() >0){
                equipmentCount = sEquipmentService.count(new LambdaQueryWrapper<SEquipment>().in(SEquipment::getMachineCabinet, machineCabinetIdList).in(SEquipment::getCreateBy , userByRoleSet));
            }else{
                equipmentCount = sEquipmentService.count(new LambdaQueryWrapper<SEquipment>().in(SEquipment::getMachineCabinet, machineCabinetIdList));
            }
        }
        ajaxResult.put("equipment",equipmentCount);

        int distributionFrameCount = 0;
        if (machineCabinetIdList != null && machineCabinetIdList.size() > 0){
            if (userByRoleSet != null && userByRoleSet.size() > 0){
                distributionFrameCount = sDistributionFrameService.count(new LambdaQueryWrapper<SDistributionFrame>().in(SDistributionFrame::getMachineCabinet, machineCabinetIdList).in(SDistributionFrame::getCreateBy, userByRoleSet));
            }else{
                distributionFrameCount = sDistributionFrameService.count(new LambdaQueryWrapper<SDistributionFrame>().in(SDistributionFrame::getMachineCabinet, machineCabinetIdList));
            }
        }
        ajaxResult.put("distributionFrame", distributionFrameCount);

        return ajaxResult;
    }
}
